package Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import rcs34.android.libs.ExifDriver.ExifDriver;
import rcs34.android.libs.ExifDriver.ExifManager;
import rcs34.android.photopublishare.GApp;


public class ImageUtils {
  private final static String LOGTAG = ImageUtils.class.getName();
  public static final String EXIF_KEY_PATH="Path";//FIXME there

  public static void copyFile(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);
    byte[] buf = new byte[1024];
    int len = 0;
    while ((len = in.read(buf)) > 0) {
      out.write(buf, 0, len);
    }
    in.close();
    out.close();
  }
  
  private static void addItem(ArrayList<String[]> _list, String[] _keyValue){
    if(_keyValue!=null){
      _list.add(_keyValue);
    }
  }

  public static ArrayList<String[]> getImageMetas(ExifDriver exifDriver)
      throws IOException {
    ArrayList<String[]> result = new ArrayList<String[]>();
    if(exifDriver==null){
      result.add(new String[]{"Format not supported","Metadata could not be extracted from the image"});
      return result;
    }
    ExifManager exifManager=new ExifManager(exifDriver,GApp.context);
    addItem(result,new String[]{"IFD0-Main image related tags","..."});
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_IMAGE_WIDTH));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_IMAGE_HEIGHT ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_BITS_PER_SAMPLE ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_COMPRESSION ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_PHOTOMETRIC_INTERPRETATION ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_ORIENTATION ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_SAMPLES_PER_PIXEL ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_PLANAR_CONFIGURATION ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_YCBCR_SUBSAMPLING ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_YCBCRPOSITIONING ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_XRESOLUTION ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_YRESOLUTION ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_RESOLUTION_UNIT ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_STRIP_OFFSETS ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_ROWS_PER_STRIP ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_STRIP_BYTECOUNTS ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_TRANSFER_FUNCTION ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_WHITE_POINT ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_PRIMARY_CHROMATICITIES ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_YCBCR_COEFICIENTS ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_REFERENCE_BLACK_WHITE ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_DATETIME ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_IMAGE_DESCRIPTION ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_MAKE ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_MODEL ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_SOFTWARE ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_ARTIST ));
    addItem(result,exifManager.getMainImageRelated(ExifDriver.TAG_COPYRIGHT ));
    // IFD Exif tags
    addItem(result,new String[]{"IFDExif-EXIF related tags","..."});
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_EXIF_VERSION ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_FLASHPIX_VERSION ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_COLOR_SPACE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_COMPONENT_CONFIGURATION ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_COMPRESSED_BITS_PER_PIXEL ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_PIXEL_X_DIMENSION ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_PIXEL_Y_DIMENSION ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_MARKER_NOTE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_USER_COMMENT ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_RELATED_SOUND_FILE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_DATETIME_ORIGINAL ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_DATETIME_DIGITIZED ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SUB_SEC_TIME ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SUB_SEC_TIME_ORIGINAL ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SUB_SEC_TIME_DIGITIZED ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_IMAGE_UNIQUE_ID ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_EXPOSURE_TIME ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_FNUMBER ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_EXPOSURE_PROGRAM ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SPECTRAL_SENSITIVITY ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_ISO_SPEED_RATINGS ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_OECF ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SHUTTER_SPEED_VALUE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_APERTURE_VALUE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_BRIGHTNESS_VALUE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_EXPOSURE_BIAS_VALUE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_MAX_APERTURE_VALUE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SUBJECT_DISTANCE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_METERING_MODE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_LIGHT_SOURCE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_FLASH ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_FOCAL_LENGTH ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SUBJECT_AREA ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_FLASH_ENERGY ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SPATIAL_FREQUENCY_RESPONSE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_FOCAL_PLANE_X_RESOLUTION ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_FOCAL_PLANE_Y_RESOLUTION ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_FOCAL_PLANE_RESOLUTION_UNIT ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SUBJECT_LOCATION ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_EXPOSURE_INDEX ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SENSING_METHOD ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_FILE_SOURCE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SCENE_TYPE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_CFA_PATTERN ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_CUSTOM_RENDERED ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_EXPOSURE_MODE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_WHITE_BALANCE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_DIGITAL_ZOOM_RATIO ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_FOCAL_LENGTH_35MM_FILM ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SCENE_CAPTURE_TYPE ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_GAIN_CONTROL ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_CONTRAST ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SATURATION ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SHARPNESS ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_DEVICE_SETTING_DESCRIPTION ));
    addItem(result,exifManager.getExifRelated(ExifDriver.TAG_SUBJECT_DISTANCE_RANGE ));
    //Thumbnail tags
    addItem(result,new String[]{"IFD1-Thumbnail related tags","..."});
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_IMAGE_WIDTH));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_IMAGE_HEIGHT ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_JPEG_INTERCHANGE_FORMAT ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_BITS_PER_SAMPLE ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_COMPRESSION ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_PHOTOMETRIC_INTERPRETATION ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_ORIENTATION ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_SAMPLES_PER_PIXEL ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_PLANAR_CONFIGURATION ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_YCBCR_SUBSAMPLING ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_YCBCRPOSITIONING ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_XRESOLUTION ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_YRESOLUTION ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_RESOLUTION_UNIT ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_STRIP_OFFSETS ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_ROWS_PER_STRIP ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_STRIP_BYTECOUNTS ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_TRANSFER_FUNCTION ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_WHITE_POINT ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_PRIMARY_CHROMATICITIES ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_YCBCR_COEFICIENTS ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_REFERENCE_BLACK_WHITE ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_DATETIME ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_MAKE ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_MODEL ));
    addItem(result,exifManager.getThumbnailRelated(ExifDriver.TAG_SOFTWARE ));
    // IFD GPS tags
    addItem(result,new String[]{"IFDGPS-GPS related tags","..."});
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_VERSION_ID ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_LATITUDE_REF ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_LATITUDE ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_LONGITUDE_REF ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_LONGITUDE ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_ALTITUDE_REF ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_ALTITUDE ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_TIME_STAMP ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_SATELITES ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_STATUS ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_MEASURE_MODE ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DOP ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_SPEED_REF ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_SPEED ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_TRACK_REF ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_TRACK ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_SLMG_DIRECTION_REF ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_SLMG_DIRECTION ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_MAP_DATUM ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DEST_LATITUDE_REF ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DEST_LATITUDE ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DEST_LONGITUDE_REF ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DEST_LONGITUDE ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DEST_BEARING_REF ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DEST_BEARING ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DEST_DISTANCE_REF ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DEST_DISTANCE ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_PROCESSING_METHOD ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_AREA_INFORMATION ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DATE_STAMP ));
    addItem(result,exifManager.getGpsRelated(ExifDriver.TAG_GPS_DIFFERENTIAL ));
    return result;
  }
}